package ui.activity.home.presenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import Bean.ProductBean;
import Bean.ProductParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.biz.ProductBiz;
import ui.activity.home.contract.ProductContract;
import ui.activity.order.OrderAct;

/**
 * Created by chenjian on 19/2/26
 */

public class ProductPresenter implements ProductContract.Presenter {

    ProductContract.View view;
    ProductBiz productBiz;


    @Inject
    public ProductPresenter(ProductContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        productBiz = (ProductBiz) biz;
    }


    @Override
    public void getPosModel(boolean isRefresh, String id) {
        view.loading(true);
        ProductParamBean productParamBean = new ProductParamBean();
        productParamBean.setId(id);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(productParamBean));
        productBiz.getPosModel(requestBody, new BaseBiz.Callback<ProductBean.DataBean>() {
            @Override
            public void onSuccess(ProductBean.DataBean info) {
                view.upDateUI(info);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(ProductBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });

    }

    @Override
    public void toOrder() {
        view.startAct(OrderAct.class, null);
    }

    @Override
    public void toSubmitOrder() {
        view.submitOrder();
    }

}
